package com.fengwk.support.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PageInfo<T> implements Serializable {

	private static final long serialVersionUID = -2877395729258294189L;

	private int pageNum;// 当前页
	private int pageSize;// 每页的数量
	private int size;// 当前页的数量
	private List<T> list;// 结果集
	private int prePage;// 前一页
	private int nextPage;// 下一页
	private boolean isFirstPage = false;// 是否为第一页
	private boolean isLastPage = false;// 是否为最后一页
	private boolean hasPreviousPage = false;// 是否有前一页
	private boolean hasNextPage = false;// 是否有下一页
	
	private int total;// 记录总数(在需要时查询)
	private int totalPage;// 总页数(在total设置时设置)

	public PageInfo(int pageNum, int pageSize) {
	    if (pageNum <= 0)
	        throw new IllegalArgumentException("pageNum <= 0");
	    if (pageSize < 0)
            throw new IllegalArgumentException("pageSize < 0");
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.prePage = pageNum - 1;
		this.nextPage = pageNum + 1;
		this.isFirstPage = pageNum == 1;
		this.hasPreviousPage = pageNum > 1;
	}

	public int queryStartRow() {
		return (pageNum - 1) * pageSize;
	}

	public int queryPageSize() {
		return pageSize + 1;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
	    if (list == null)
	        list = new ArrayList<T>();
	    
	    int size = list.size();
		if(size == queryPageSize()) {
		    this.hasNextPage = true;
		    list.remove(size - 1);
		} else 
            this.isLastPage = true;
		
		this.size = size;
		this.list = list;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public boolean isFirstPage() {
		return isFirstPage;
	}

	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public boolean isLastPage() {
		return isLastPage;
	}

	public void setLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
		this.total = total;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
}
